*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Linea Hasager																			*
* DESCRIPTION:		Demeaned monthly admissions for balance tests									 		*
* CREATED:			Nov. 8, 2018																		   	*
* LAST MODIFIED:	May 26, 2022														       				*
*----------------------------------------------------------------------------------------------------------	*

clear 
set more off

// Directories
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"



*--------------------------------------------------------------------------------------------------------	

/* LOAD DATA*/
use "${in}\Population_full", replace


*DROP IF OLDER THAN 49
keep if age< 50

gen somalia=0
replace somalia=1 if opr_land==5289
label var somalia "Somalia"

*DROP IF FROM SOMALIA
keep if somalia==0


*MONTH
gen month_nr=mofd(tilladelsesdato)
format %tmMon_CCYY month_nr
label variable month_nr "Month"

*MONTH
gen m_nr=mofd(tilladelsesdato)-468
label variable m_nr "Months Since January 1st 1999"


*ADMISSIONS PER MONTH
bysort month_nr: egen antal=count(pnr)
label variable antal "Admissions per month"


gen month=month(tilladelsesdato)



*GENERATE DEMEANED ADMISSIONS PER MONTH

bysort year_admission: egen n_year=count(pnr)
gen share=antal/n_year

local cutoff 2001

preserve
drop if year_admission >= `cutoff'
drop if (m_nr >= -6 & m_nr <=5)
keep m_nr antal month
duplicates drop
collapse (mean) antal, by(month)
rename antal antal_mean
save "${temp}\means", replace
restore


preserve
drop if year_admission >= `cutoff'
drop if (m_nr >= -6 & m_nr <=5)
keep m_nr share month
duplicates drop
collapse (mean) share, by(month)
rename share share_mean
save "${temp}\shares", replace
restore


merge m:1 month using "${temp}\means"
drop _merge

merge m:1 month using "${temp}\shares"
drop _merge

sort tilladelsesdato


gen share_demeaned=share-share_mean



*GRAPH DEMEANED MONTHLY ADMISSIONS
preserve
keep if m_nr <27
collapse (mean) share_demeaned, by(m_nr)
twoway scatter share_demeaned m_nr, yline(0, lcolor(black) lpattern(dash)) xline(0, lcolor(black) lpattern(dash)) tlabel(-24 -12 0 12 24) xtitle("Month of Admission") ytitle("Share") graphregion(fcolor(white) margin(medlarge)) mcolor(gs10) ylab(-0.15(0.05)0.15, format(%10.2fc))
graph export "${output}\oa_figure1b.png", as(png) replace width(5000)
 restore





******************************************************
*DEMEANED ADMISSIONS FOR BALANCE TEST TABLE
******************************************************

keep if m_nr <27
collapse (mean) share_demeaned antal, by(m_nr)


*----------------------------------------------------------------------------------------------------------	*
* CALCULATE MEAN AND STANDARD DEVIATION 
*----------------------------------------------------------------------------------------------------------	*

preserve
egen mean=mean(antal)
egen sd=sd(antal)
keep mean sd
keep if _n==1
gen variable="antal"
save "${temp}\admissions.dta", replace
restore

foreach var in share_demeaned {
preserve
egen mean=mean(`var') 
egen sd=sd(`var') 
keep mean sd
keep if _n==1
gen variable="`var'"
append using "${temp}\admissions.dta"
save "${temp}\admissions.dta", replace
restore
}



set more off
foreach var in antal {
preserve
rdrobust `var' m_nr,  kernel(tri) c(0) p(1) 
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_admissions.dta", replace ci addlabel(variable,`var') 
restore
}

foreach var in share_demeaned {
preserve
rdrobust `var' m_nr,  kernel(tri) c(0) p(1) 
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_admissions.dta", append ci addlabel(variable, `var')
restore
}


*CCOMBINE SUMMARY STATISTICS AND RD BALANCE ESTIMATES

clear
use  "${temp}\admissions.dta"
merge 1:1 variable using "${temp}\RD_admissions.dta"
drop N var _merge


format mean sd coef stderr ci_lower ci_upper %10.2fc


tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

gen sortvar=1 if variable=="antal"
replace sortvar=3 if variable=="share_demeaned"





replace variable="Monthly Admissions" if variable=="antal"
replace variable="Demeaned Monthly Share of Annual Admissions" if variable=="share_demeaned"


sort sortvar


*OUTPUT TABLE
listtex variable mean sd coef ci using "${output}\table1a_admissions.tex", ///
rstyle(tabular) replace



****************************************
*	DELETE TEMPORARY DATA
****************************************

erase "${temp}\means.dta"
erase "${temp}\shares.dta"
erase "${temp}\RD_admissions.dta"
erase "${temp}\admissions.dta"
